package com.kedll.library.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.kedll.library.R;


/**
 * Created by Administrator on 2015/12/8.
 */
public class ChoiceMenuDialogFromBottom {
    private Context context;
    private Dialog dialog;
    private View contentView;
    private int gravity = Gravity.BOTTOM;

    public ChoiceMenuDialogFromBottom(Context context) {
        if (context != null) {
            this.context = context;
            init();
        }
    }

    private void init() {
        if (dialog == null && context != null) {
            dialog = new Dialog(context, R.style.my_dialog);
            dialog.setCanceledOnTouchOutside(true);
        }
    }

    public void setContentView(View contentView){
        this.contentView = contentView;
        if (contentView != null && dialog != null){
            dialog.setContentView(contentView);
            Window window = dialog.getWindow();
            window.setGravity(gravity);
            window.setWindowAnimations(com.kedll.library.R.style.timepopwindow_anim_style);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = context.getResources().getDisplayMetrics().widthPixels;
            window.setAttributes(lp);
        }
    }

    public void setGravity(int gravity) {
        if (dialog != null) {
            Window window = dialog.getWindow();
            window.setGravity(gravity);
            window.setWindowAnimations(R.style.timepopwindow_anim_style);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = context.getResources().getDisplayMetrics().widthPixels;
            window.setAttributes(lp);
        }
    }

    public void show(){
        if (dialog == null){
            init();
        }
        if (dialog != null){
            dialog.show();
        }
    }

    public void dismiss(){
        if (dialog != null){
            dialog.dismiss();
            dialog = null;
            context = null;
        }
    }
}
